package com.example.program.demos.controller;

import com.example.program.demos.request.ProgramParams;
import com.example.program.demos.service.ReportService;
import com.example.program.exception.ErrorRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/report")
public class ReportController {
    @Autowired
    ReportService reportService;

    private static final String CONTENT_TYPE = "application/vnd.ms-excel;charset=utf-8";

    /**
     * 批量导出工资表
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/exportBatchSalary", method = RequestMethod.GET, produces = "application/vnd.ms-excel;charset=utf-8")
    public void exportBatchSalary(HttpServletRequest request, HttpServletResponse response) throws Exception{
        response.setHeader("Content-Disposition","attachment;filename=工资表.xlsx");
        String[] groups = request.getParameter("group").toString().split(",");
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", request.getParameter("companyId"));
        map.put("programId", request.getParameter("programId"));
        map.put("month", request.getParameter("month"));
        reportService.exportMulitSheetsToSalary(response.getOutputStream(), groups, map);

    }


    /**
     * 合并导出工资表
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/exportMergeSalary", method = RequestMethod.GET, produces = "application/vnd.ms-excel;charset=utf-8")
    public void exportMergeSalary(HttpServletRequest request, HttpServletResponse response) throws Exception{
        response.setHeader("Content-Disposition","attachment;filename=工资表.xlsx");
        String reGroup = request.getParameter("reGroup").toString();
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", request.getParameter("companyId"));
        map.put("programId", request.getParameter("programId"));
        map.put("month", request.getParameter("month"));
        map.put("group", request.getParameter("group"));
        reportService.exportMergeSalary(response.getOutputStream(), reGroup, map);

    }

    /**
     * 批量导出考勤表
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/exportBatchAttendance", method = RequestMethod.GET, produces = "application/vnd.ms-excel;charset=utf-8")
    public void exportBatchAttendance(HttpServletRequest request, HttpServletResponse response) throws Exception{
        response.setHeader("Content-Disposition","attachment;filename=考勤表.xlsx");
        String[] groups = request.getParameter("group").toString().split(",");
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", request.getParameter("companyId"));
        map.put("programId", request.getParameter("programId"));
        map.put("month", request.getParameter("month"));
        reportService.exportBatchAttendance(response.getOutputStream(), groups, map);

    }

    /**
     * 合并导出考勤表
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/exportMergeAttendance", method = RequestMethod.GET, produces = "application/vnd.ms-excel;charset=utf-8")
    public void exportMergeAttendance(HttpServletRequest request, HttpServletResponse response) throws Exception{
        response.setHeader("Content-Disposition","attachment;filename=考勤表.xlsx");
        String reGroup = request.getParameter("reGroup").toString();
        Map<String, Object> map = new HashMap<>();
        map.put("companyId", request.getParameter("companyId"));
        map.put("programId", request.getParameter("programId"));
        map.put("month", request.getParameter("month"));
        map.put("group", request.getParameter("group"));
        reportService.exportMergeAttendance(response.getOutputStream(), reGroup, map);

    }

}
